﻿/*
7-33 有理数加法 (15 分)
本题要求编写程序，计算两个有理数的和。

输入格式：
输入在一行中按照a1/b1 a2/b2的格式给出两个分数形式的有理数，其中分子和分母全是整形范围内的正整数。

输出格式：
在一行中按照a/b的格式输出两个有理数的和。注意必须是该有理数的最简分数形式，若分母为1，则只输出分子。

输入样例1：
1/3 1/6
输出样例1：
1/2
输入样例2：
4/3 2/3
输出样例2：
2
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct _Rational {
	long long numerator;
	long long denominator;
} Rational, *PRational;

int gcd(int a, int b) {
	int c;
	if (b > a)
	{
		c = a;
		a = b;
		b = c;
	}
	c = a % b;
	while (c) {
		a = b;
		b = c;
		c = a % b;
	}
	return b;
}

static void rational_read(PRational r) {
	scanf("%lld/%lld", &(r->numerator), &(r->denominator));
}

static void rational_print(PRational r) {
	if (r->denominator > 1)
		printf("%lld/%lld\n", (r->numerator), (r->denominator));
	else
		printf("%lld\n", r->numerator);
}

static Rational rational_add(PRational a, PRational b) {
	Rational c = {};
	c.denominator = a->denominator*b->denominator;
	c.numerator = a->numerator * b->denominator + a->denominator * b->numerator;
	int d = gcd(c.denominator, c.numerator);
	c.denominator /= d;
	c.numerator /= d;
	return c;
}

int main() {
	freopen("D:/Develop/GitRepos/MOOC/浙江大学/数据结构/201906/zju_C_Basic/data/7.33.txt", "r", stdin);
	int n=0;
	Rational a = {}, b = {}, c = {};
	rational_read(&a);
	rational_read(&b);
	c = rational_add(&a, &b);
	//rational_print(&a);
	//rational_print(&b);
	rational_print(&c);
	return 0;
}